// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import store from './vuex/index'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//导入fontawesome
import { library } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
//引入图标
import {
  faThumbsUp,//点赞人数图标
  faEye,//观看人数图标
  faBell,//消息图标
} from '@fortawesome/free-regular-svg-icons';
library.add(
  faThumbsUp,
  faEye,
  faBell,
);

// 使用element ui
Vue.use(ElementUI)
//注册Font Awesome组件
Vue.component('font-awesome-icon', FontAwesomeIcon);

Vue.config.productionTip = false

/*创建axios对象*/
let http = axios.create();
//设置请求拦截器:会在每一次真的发送请求之前执行，这里将token设置到请求头
//达到每次请求自动携带token
http.interceptors.request.use(
    config => {
        console.log("请求前执行")
        //1.得到token
        let token = window.sessionStorage.getItem("token");
        //2.判断，如果不为空，则设置到请求头里面
        if (token) {
            config.headers.authorization = token;
        }
        //请求放行
        return config;

    }
);
//响应拦截器，判断响应头中是否携带token
http.interceptors.response.use(
    response => {
        console.log("请求后执行")
        if (response.data.status == "NO_LOGIN"||response.data.status == "TOKEN_ERROR") {
            //跳到登录页面
            router.push("/login");
        }
        //1.从响应头获取token'
        let token = response.headers.authorization;
        if (token) {
            window.sessionStorage.setItem("token", token);
        }
        return response;
    }
);


Vue.prototype.axios = http
Vue.prototype.baseUrl = "http://localhost:8080/"
Vue.prototype.$store = store;



/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    //store,
    components: { App },
    template: '<App/>'
})
